package com.jrapider.designer.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jrapider.compent.dict.DictData;
import com.jrapider.compent.dict.IDictProvider;
import com.jrapider.exception.TransRollbackException;
import com.jrapider.mapper.SysEntityFieldMapper;
import com.jrapider.mapper.SysEntityMapper;
import com.jrapider.om.PageInfo;
import com.jrapider.om.SysEntity;
import com.jrapider.om.SysEntityField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2018/11/17.
 */
@Service
public class SysEntityFieldService extends ServiceImpl<SysEntityFieldMapper, SysEntityField> implements IDictProvider {

    @Autowired
    private SysEntityMapper entityMapper;

    public Long saveAndGetId(SysEntityField sysEntityField) {
        SysEntityField condition = new SysEntityField();
        condition.setColumnEx(sysEntityField.getColumnEx());
        condition.setEntityId(sysEntityField.getEntityId());
        SysEntityField data = baseMapper.selectOne(new QueryWrapper<>(condition));
        if (data == null) {
            sysEntityField.setField(StringUtils.underlineToCamel(sysEntityField.getColumnEx()));
            sysEntityField.setNameEx(sysEntityField.getCommentEx());
            baseMapper.insert(sysEntityField);
        } else {
            baseMapper.updateById(sysEntityField);
        }
        return sysEntityField.getId();
    }

    @Override
    public Set<String> supportCodes() {
        return new HashSet<String>() {{
            add("field");
        }};
    }

    @Override
    public List<DictData> getDict(String s) {
        List<SysEntityField> list = baseMapper.selectList(new QueryWrapper<SysEntityField>());
        return list.stream().map(field -> new DictData() {{
            setValue(field.getField());
            setCode("field");
            setNote(field.getField() + "|" + field.getNameEx());
            setSpell(field.getEntityId().toString());
        }}).collect(Collectors.toList());
    }

    @Override
    public List<DictData> getDict(String code, String filter, PageInfo pageInfo) {
        return getDict(code).stream()
                .filter(dict -> {
                    if (StringUtils.isEmpty(filter)) {
                        return true;
                    } else {
                        return filter.equals(dict.getSpell());
                    }
                }).collect(Collectors.toList());
    }
    @Transactional(rollbackFor = TransRollbackException.class)
    public boolean saveOrUpdateBatch(SysEntity sysEntity, List<SysEntityField> sysEntityFields) {
        entityMapper.updateById(sysEntity);
        saveOrUpdateBatch(sysEntityFields);
        return true;
    }
}
